package firbi.base.display;

import java.util.ArrayList;
import java.util.Iterator;

import android.content.Intent;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;

import firbi.base.util.Constants;


/** Manages the BusStopOverlayItems on the map. 
 *  Maintains a list of overlays and a map view that
 *  the overlays are to displayed on. 
 *
 */

public class BusStopOverlay extends ItemizedOverlay<BusStopOverlayItem> {
	private ArrayList<BusStopOverlayItem> mBusOverlays = new ArrayList<BusStopOverlayItem>();
	private MapView mMapView;
	
	public BusStopOverlay(Drawable defaultMarker) {
		super(boundCenter(defaultMarker));
	}
	@Override
	protected BusStopOverlayItem createItem(int i) {
	  return mBusOverlays.get(i);
	}
	@Override
	public int size() {
		return mBusOverlays.size();
	}
	public void addOverlay(BusStopOverlayItem overlay) {
	    mBusOverlays.add(overlay);
	    populate();
	}

	/** Removes all overlay items from the current array */
	public synchronized void removeAll() {
		Iterator<BusStopOverlayItem> it = mBusOverlays.iterator();
		while (it.hasNext()) {
			it.next();
			it.remove();
		}
	}
	
	/** Sets the Map View of this overlay */
	public void setMap(MapView map) {
		mMapView = map;
	}
	/** 
	 * When a bus stop icon is touched the application will
	 * go to another view that displays the routes of that bus stop.
	 */
	@Override
	protected boolean onTap(int index) {
		 Intent i = new Intent();
		 i.putExtra(Constants.sSTOP_KEY, mBusOverlays.get(index).getStopId());
		 i.setClassName("firbi.base", "firbi.base.StopView");
		 mMapView.getContext().startActivity(i);
		 return true;
	}
}
